<template>
  <div class="dashboard-container">
    <div class="app-container">
      <page-tool :show-before="false">
        <template #after>
          <el-button type="primary" size="small" @click="onAdd({type:1, pid: '0'})">添加权限</el-button>
        </template>
      </page-tool>
      <!-- el-table[:data="list" :border="true"]>el-table-column[label prop=""]{}*4 -->
      <el-table :data="list" :border="true" row-key="id">
        <el-table-column label="名称" prop="name"></el-table-column>
        <el-table-column label="标识" prop="code"></el-table-column>
        <el-table-column label="描述" prop="description"></el-table-column>
        <el-table-column label="操作">
          <!-- template[v-slot="{row}"]>el-button[type="text"]*3 -->
          <template v-slot="{row}">
            <el-button type="text" @click="onAdd({type:2,pid:row.id})">添加</el-button>
            <el-button type="text" @click="onUpdate(row.id)">编辑</el-button>
            <el-button type="text" @click="onDel(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <add-perm v-if="showDialog" ref="addPermRef" :visible="showDialog" width="50%" @close="showDialog = false"></add-perm>
  </div>
</template>

<script>
import { delPermission, getPermissionDetail, getPermissionList } from '@/api/permisson.js'
import { translateListToTree } from '@/utils'
import AddPerm from './components/add-perm.vue'
export default {
  name: 'PermissionPage',
  components: { AddPerm },
  data() {
    return {
      list: [],
      showDialog: false
    }
  },
  created() {
    this.getPermissionList()
  },
  mounted() {},
  methods: {
    async getPermissionList() {
      this.list = translateListToTree(await getPermissionList(), '0')
    },
    onAdd(item) {
      this.showDialog = true
      this.$nextTick(() => {
        Object.assign(this.$refs.addPermRef.form, item)
      })
    },
    async onUpdate(id) {
      const loading = this.$loading({ customClass: 'loading-box' })
      this.showDialog = true
      await this.$nextTick()
      this.$refs.addPermRef.form = await getPermissionDetail(id)
      loading.close()
    },
    onDel(id) {
      // 确认删除,elmecon
      this.$confirm('确认删除?', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async action => {
        await delPermission(id)
        this.$message.success('操作成功')
        this.getPermissionList()
      }).catch(() => {

      })
    }
  }
}
</script>

<style lang="scss">
.loading-box{
  z-index: 3000 !important;
}
</style>
